package com.chen.blue.listener;

import com.chen.blue.listener.event.SystemConnectValid;
import com.chen.blue.sysconfig.SystemConfig;
import com.chen.blue.utils.SpringUtil;
import io.minio.MinioClient;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.InetAddress;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Objects;

/**
 * Created By CLT On 2024/5/16.
 */
@SuppressWarnings("all")
@Slf4j
public class SystemConnect {

    private RedisTemplate redisTemplate = SpringUtil.getBean("redisTemplate");

    private SqlSession sqlSession = SpringUtil.getBean(SqlSession.class);

    private SystemConfig systemConfig = SpringUtil.getBean(SystemConfig.class);

    public void vaildContect() throws SQLException {

        RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection();
        if ("PONG".equals(redisConnection.ping())) {
            log.info("\t๑◔‿◔๑  Redis 连接成功！");
        } else {
            log.error("\t,,Ծ‸Ծ,,  Redis 连接失败！");
        }
        Connection mySqlConnect = sqlSession.getConnection();
        if (Objects.nonNull(mySqlConnect)) {
            log.info("\t๑◔‿◔๑  MySQL 连接成功！");
            mySqlConnect.close();
        } else {
            log.error("\t,,Ծ‸Ծ,,  MySQL 连接失败！");
        }

        MinioClient client = MinioClient.builder().endpoint(systemConfig.getMinioEndpoint()).credentials(systemConfig.getMinioAccessKey(), systemConfig.getMinioSecretKey()).build();
        try {
            client.listBuckets();
            log.info("\t๑◔‿◔๑  Minio 连接成功！");
        } catch (Exception e) {
            log.error("\t,,Ծ‸Ծ,,  Minio 连接失败！");
            e.printStackTrace();
        }

    }

}
